//  json web token  jwt
let token;
const api = require('./api.js')

export function call(option) {
    return new Promise((resolve, reject) => {
        if (!option.data) option.data = {};
        if (token) option.data.token = token;
        uni.showLoading();
        uniCloud.callFunction({
            name: option.name,
            data: option.data,
            success: (res) => {
                // api.verifyToken(res.result.token)
                if (res.result && res.result.token) token = res.result.token;
                if (option.success) option.success(res);
                resolve(res);
            },
            fail: (err) => {
                if (option.fail) option.fail(err);
                reject(err);
            },
            complete: () => {
                uni.hideLoading();
                if (option.complete) option.complete();
            }
        });
    });
}



export function getUserOpenid() { // 用于修改后获取最新的个人信息
    uni.login({
        provider: 'weixin',
        success: (res) => {
            var code = res.code
            call({ // 使用封装好的 
                name: "login",
                data: {
                    api: 'getUserOpenid',
                    code
                },
                success: (res) => {
                    console.log(res.result);
                    getApp().globalData.userInfo = res.result.data[0]
                    uni.setStorageSync("userInfo", res.result.data[0])
                    getApp().globalData.token = res.result.data[0].token
                    uni.setStorageSync("token", res.result.data[0].token)
                }
            })
        }
    })
}
